<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="特约商户:" prop="userId">
        <el-select v-model="queryParams.userId" clearable placeholder="请选择特约商户">
          <el-option
            v-for="item in userDataList"
            :key="item.userId"
            :label="item.phonenumber + ' (' + item.userName + ')'"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="活动名称:" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动名称"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery">
          <el-button circle slot="append" icon="el-icon-search" @click="handleQuery"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="清算状态" prop="settlementType">
        <el-select v-model="queryParams.settlementType" placeholder="请选择状态" clearable size="small">
          <el-option key="2" label="已处理" :value="2"/>
          <el-option key="3" label="未处理" :value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="confirmSettlement"
        >同意清算</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" row-key="id" @selection-change="handleSelectionChange">
      <el-table-column type="selection" :selectable="checkSelectable" width="50" align="center" />
      <el-table-column label="商户账号" prop="phonenumber"/>
      <el-table-column label="商户名称" prop="userName"/>
      <el-table-column label="活动名称" prop="title"/>
      <el-table-column label="清算状态" prop="settlementType">
        <template slot-scope="scope">
          {{ scope.row.settlementType === 3 ? '申请清算':'已清算' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.settlementType === 3"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="confirmSettlement(scope.row)"
          >同意清算</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {sysUserList} from "@/api/triones/settledStore";
import {applySettlementPageList, settlement} from "@/api/triones/activity";
export default {
  name: "index",
  dicts: ['settlement_type'],
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        userId: undefined,
        settlementType: undefined
      },
      userDataList: [],
      loading: true,
      total: 0,
      dataList: [],
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
    }
  },
  created() {
    this.getList();
    this.getSysUserList();
  },
  methods: {
    getSysUserList() {
      sysUserList().then(response => {
        this.userDataList = response.data;
      })
    },
    getList() {
      this.loading = true;
      applySettlementPageList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    confirmSettlement(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认同意清算?').then(function() {
        return settlement(ids);
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('清算成功')
      }).catch(() => {
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    checkSelectable(row) {
      return row.settlementType === 3;
    },
  }
}
</script>

<style scoped>

</style>
